package com.wisewin.circle.api.controller;

import com.wisewin.circle.api.controller.base.BaseController;
import com.wisewin.circle.config.dto.ResultDTO;
import com.wisewin.circle.config.dto.ResultDTOBuilder;
import com.wisewin.circle.entity.LoginUser;
import com.wisewin.circle.entity.bo.QueryParamBO;
import com.wisewin.circle.entity.db.UserFollowDO;
import com.wisewin.circle.service.UserFollowService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/userFollow")
public class UserFollowController extends BaseController {
    final static Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserFollowService userFollowService;

    // 添加关注
    @PostMapping("/addUserFollow")
    public ResultDTO addUserFollow (HttpServletRequest request, @RequestBody UserFollowDO userFollowDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        userFollowDO.setUserId(loginUser.getId());
        return userFollowService.addUserFollow(userFollowDO);
    }

    // 取消关注
    @PostMapping("/cancleUserFollow")
    public ResultDTO cancleUserFollow (HttpServletRequest request, @RequestBody UserFollowDO userFollowDO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        userFollowDO.setUserId(loginUser.getId());
        return userFollowService.cancleUserFollow(userFollowDO);
    }

    // 查询我的关注列表
    @PostMapping("/queryUserFollowList")
    public ResultDTO queryUserFollowList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        queryParamBO.setUserId(loginUser.getId());
        return userFollowService.queryUserFollowList(queryParamBO, getPageQuery(queryParamBO.getPageNo(), queryParamBO.getPageSize()));
    }

    // 查询我的粉丝列表
    @PostMapping("/queryUserFansList")
    public ResultDTO queryUserFansList (HttpServletRequest request, @RequestBody QueryParamBO queryParamBO) {
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            return ResultDTOBuilder.failure("0000005", "必传参数为空", null);
        }
        // 获取登录用户
        LoginUser loginUser = super.getLoginUser(token);
        if (loginUser == null ) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001", "登录已过期", null);
        }
        queryParamBO.setUserId(loginUser.getId());
        return userFollowService.queryUserFansList(queryParamBO, getPageQuery(queryParamBO.getPageNo(), queryParamBO.getPageSize()));
    }
}
